var spider = require('./node.spider');
var cheerio = require('cheerio');
var url_node = require('url');
var Iconv = require('iconv-lite');
var pinyin = require('node-pinyin');
var insertDB = require('./insert.db');
var utils = require('../../dao/utils');

function sort(title) {
    title = title.replace(/(^s*)|(s*$)/g, "");
    let str = title.substring(0, 1).toUpperCase();

    // 英文字符，数字
    if (str >= 'A' && str <= 'Z') {
        return str;
    } else if (str >= 0 && str <= 9) {
        return '0-9';
    } else {

        var p = pinyin(str, { "style": "normal" })[0][0].toUpperCase();
        // console.log(p);
        p = p.substring(0, 1).toUpperCase();


        // console.log((p >= 'A') && (p <= 'Z'));

        if ((p >= 'A') && (p <= 'Z')) {
            return p;
        } else {
            return '其他';
        }


    }


}

// setting
var setting = {
    start: {
        url: 'http://www.1kkk.com/manhua-jpkr-p*PageReplace*/',
        pageItem: 1,
        curPage: 1,
        totalPage: 259,
        timeout: 5000
    },
    parseHandel: function (body, url, _default) {
        var res;
        try {
            res = body.toString('utf-8');
        } catch (e) {
            spider.spider(url, _default);
            return;
        }


        var $ = cheerio.load(res);

        kkkHtml($, url, _default);

        // console.log(data);

        // if (_default.start.curPage == 1) {
        //     _default.start.totalPage = $('#search_fy a').eq(-2).text().replace(/(^s*)|(s*$)/g, "");
        // }

        _default.pageNext(_default);

    },
    parseDetailHandel: function (body, url, _default) {
        var res;
        try {
            res = body.toString('utf-8');
        } catch (e) {
            spider.spiderDetail(url, _default);
            return;
        }
        // console.log(res);

        var $ = cheerio.load(res);

        var data = kkkDetailHtml($, url, _default);

        if (data == false) {
            return;
        }

        insertDB(data, _default);
        // console.log(data);
    }


};

spider.init(setting);



// 1kkk网站爬取
function kkkHtml($, url, _default) {

    var urlObj = url_node.parse(url);
    var protocol = urlObj.protocol;
    var hostname = urlObj.hostname;

    var host = protocol + "//" + hostname;

    var liArry = $('.mh-list.col7 li');

    if (liArry.length == 0) {
        console.log('no lis');
        return false;
    }

    for (var i = 0; i < liArry.length; i++) {
        var item = liArry.eq(i);
        if (item.hasClass('ad_360_item')) {
            continue;
        }
        var url = host + item.find('.mh-item-detali .title a').eq(0).attr('href');
        var title = item.find('.mh-item-detali .title a').eq(0).text().replace(/(^s*)|(s*$)/g, "");
        //detail  
        spider.spiderDetail(url, _default, 5000, title);
    }

}

function kkkDetailHtml($, url, _default) {
    var urlObj = url_node.parse(url);
    var protocol = urlObj.protocol;
    var hostname = urlObj.hostname;

    var host = protocol + "//" + hostname;

    var item = $('#tempc .detail-list-title a');

    if (item.length == 0) {
        return false;
    }

    var chapter = [];


    var chaptersLis = $("#detail-list-select-1 li");


    for (var i = 0; i < chaptersLis.length; i++) {
        var _cur = chaptersLis.eq(i).find('a');

        chapter.push({
            href: host + utils.trim(_cur.attr('href')),
            chapter: utils.trim(_cur.text()).replace(/\s+/g,"")
        });
    }
    // var _sChapter = chapter.join(',');
    var _news = $('#tempc .detail-list-title .s a').text().replace(/(^\s*)|(\s*$)/g, "");
 


    var score = utils.trim($('.banner_detail_form .info .title .score').text());

    var _title = $('.banner_detail_form .info .title').text().replace(score, "");
    _title = utils.trim(_title);

    // info
    var info = $('.banner_detail_form .info .content').text().replace(/[\r\n]/g, "");

    if (info.indexOf('[+展开]') != -1) {
        info = info.replace('[+展开]', '');
        info = info.replace('[-折叠]', '');
    }





    var time = $('#tempc .detail-list-title .s span').text();

    var cutWord = $('#tempc .detail-list-title .s span a').text();

    time = time.replace(cutWord,'');

    time = utils.trim(time);

    time = utils.timeFormat(time);

    var data = {
        title: _title,
        href: url,
        img: $('.banner_detail_form .cover img').attr('src'),
        auth: $('.banner_detail_form .info .subtitle a') ? $('.banner_detail_form .info .subtitle a').eq(0).text().replace(/(^s*)|(s*$)/g, "") : null,

        auth_href: host + $('.banner_detail_form .info .subtitle a') ? $('.banner_detail_form .info .subtitle a').eq(0).attr('href') : null,

        news: chapter.length>0? chapter[0]['chapter']:null,
        news_href: chapter.length>0?chapter[0]['href']:null,

        isFinish: utils.trim($('.banner_detail_form .info .tip .block').eq(0).find('span').text()) == '已完结' ? true : false,

        updateTime: time,

        type: $('.banner_detail_form .info .tip .block').eq(1).find('span').text(),

        letter: sort(_title),

        info: info,
        domain: hostname,
        isUpdate: true,
        "chapters": chapter
    };

    
    return data;
    // console.log(chapter);

}

